<template>
  <div>
    <el-dialog
      :title="batchDataListId.length < 1 ? '设置接警时段' : '批量设置接警时段'"
      :visible.sync="dialogVisible"
      width="550px"
      :before-close="closeDialog"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-alert
        title="默认全天接警，设置接警时段后只在时段内接收告警通知"
        type="warning"
        center
        show-icon
        :closable="false"
        style="margin-bottom: 20px"
      >
      </el-alert>
      <el-form :model="addForm" :rules="rules" ref="refForm" label-width="80px">
        <div
          v-for="(item, index) in addForm.formData"
          :key="index"
          class="list"
        >
          <el-form-item size="small" label="接警时段">
            <div class="handle-item">
              <el-time-picker
                value-format="HH:mm:ss"
                is-range
                v-model="item.value1"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
              >
              </el-time-picker>
              <div class="handle">
                <i
                  class="el-icon-delete icon"
                  @click="clickDel(index)"
                  v-if="addForm.formData.length > 1"
                ></i>
                <i
                  class="el-icon-circle-plus-outline icon icon1"
                  @click="clickAdd(index)"
                  v-if="index === addForm.formData.length - 1"
                ></i>
              </div>
            </div>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog" class="cancel-button">取 消</el-button>
        <el-button
          size="small"
          @click="submit"
          v-preventReClick="1000" class="confirm-button"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqReceiverTimeUpdate, reqGetByReceiverId } from "@/api/api";

export default {
  props: ["spaceData", "batchDataListId"],
  mixins: [],
  data() {
    return {
      dialogVisible: false,
      addForm: {
        formData: [
          {
            value1: ["", ""],
          },
        ],
      },
      rules: {
        value1: [
          {
            required: true,
            message: "请选择有效期",
            trigger: "change",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {
    dialogVisible(e) {
      if (e && this.batchDataListId.length < 1) {
        // console.log(this.batchDataListId);
        this.GetByReceiverId();
      }
    },
  },
  methods: {
    // 查询接警人接警时段
    async GetByReceiverId() {
      const res = await reqGetByReceiverId(this.spaceData.id);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      // console.log(res);
      if (res.data.rows.length == 0) return;
      this.addForm.formData = [];
      res.data.rows.forEach((item) => {
        this.addForm.formData.push({
          value1: [item.startTime, item.endTime],
        });
      });
    },
    // 点击删除
    clickDel(index) {
      this.addForm.formData.splice(index, 1);
    },
    // 点击新增
    clickAdd() {
      if (this.addForm.formData.length >= 3)
        return this.$modal.notifyWarning("最多添加3个时间段");
      this.addForm.formData.push({
        value1: ["00:00:00", "23:59:59"],
      });
    },
    // 点击确定
    async submit() {
      // console.log(this.addForm.formData);
      // 处理数据
      let flag = false;
      let list = [];
      this.addForm.formData.forEach((item) => {
        item.value1 = item.value1 || [];
        if (item.value1[0]) {
          if (item.value1[0] == item.value1[1]) {
            flag = true;
          }
          list.push({
            startTime: item.value1[0],
            endTime: item.value1[1],
          });
        }
      });
      if (flag)
        return this.$modal.notifyWarning("开始时间和结束时间相同，请修改！");
      // console.log(list);
      const res = await reqReceiverTimeUpdate({
        receiverIdList:
          this.batchDataListId.length < 1
            ? [this.spaceData.id]
            : this.batchDataListId,
        formData: list,
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.$modal.notifySuccess("设置成功");
      this.closeDialog();
      this.$parent.clearSelections();
    },
    // 点击关闭弹框
    closeDialog() {
      this.dialogVisible = false;
      this.addForm = {
        formData: [
          {
            value1: ["", ""],
          },
        ],
      };
    },
  },
  deactivated() {
    if(this.$refs.refForm!=null){
      this.closeDialog();
    }
  },
};
</script>

<style lang="less" scoped>
@import url("../../common.less");

::v-deep .el-dialog__body {
  padding: 10px 20px;
}

.handle-item {
  width: 450px;
  display: flex;
  align-items: center;

  .handle {
    width: 70px;
    color: #8074fb;
    margin-left: 10px;
    display: flex;
    align-items: center;

    .icon {
      cursor: pointer;
      font-size: 23px;
    }

    .icon1 {
      margin-left: 10px;
    }
  }
}

// 日期控件颜色
::v-deep .el-range-editor .el-range-input {
  color: @primaryContentTxt !important;
  background-color: transparent;
}

// 时间日期 至
::v-deep .el-range-editor--small .el-range-separator {
  line-height: 26px;
  color: @primaryContentTxt !important;
}
</style>
